(function($){
    $(document).ready(function () {
        /*模板引擎设置*/
        _.templateSettings = {
            interpolate: /\{\{(.+?)\}\}/g
        };

        _.extend(Backbone.Model.prototype,Backbone.Validation.mixin);

        var BuyerModel=Backbone.Model.extend({
            defaults:{
                name:"",
                age:""
            },

            validation:{
                name:{
                    required:true
                },
                age:{
                    min:18
                }
            }
        });

        var BuyerModelFormView=Backbone.View.extend({
            initialize:function(){
                Backbone.Validation.bind(this);
            },

            template: _.template('<form>Enter Name:<input type="text" name="name" value="{{name}}"/><br/>' +
                                 'Enter age:<input type="text" name="age" value="{{age}}"/><br>' +
                                 '<input type="button" name="save" value="save"/></form>'),

            render:function(){
                var html=this.template(this.model.toJSON());
                $(this.el).html(html);

            },

            events:{
                'click[name~="save"]':'save'
            },

            save:function(){
                this.model.set({
                    name:$('[name~="name"]').val(),
                    age:$('[name~="age"]').val()
                });
            }
        });

        var buyerModel=new BuyerModel();
        var buyerModelFormView=new BuyerModelFormView({
            model:buyerModel,
            el:"#res"
        });
        buyerModelFormView.render();
    });
})(jQuery);